package cn.project.engineering

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkRequest
import android.os.Build
import android.os.Bundle
import androidx.annotation.RequiresApi
import cn.project.engineering.http.NetworkCallbackImpl
import cn.project.engineering.service.LoopJobService
import cn.project.engineering.utils.MyCrashHandler
import cn.project.engineering.utils.WonderSp
import com.lfh.custom.common.util.AppUtil
import com.tencent.bugly.crashreport.CrashReport
import log.KLog


class GwApplication : Application() {
    companion object {

        private val TAG = "MyApplication"


    }
    @SuppressLint("MissingPermission")
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onCreate() {
        super.onCreate()
        KLog.init(BuildConfig.DEBUG)
        ininState()
        MyCrashHandler.register(this)
        //region bugly 初始化
        val processName = AppUtil.getProcessName(android.os.Process.myPid())
        val strategy = CrashReport.UserStrategy(applicationContext)
        strategy.isUploadProcess = null != processName || processName == packageName
        CrashReport.initCrashReport(applicationContext, "1b03333c98", true, strategy)
        //endregion
        LoopJobService.startDaemon(this)
        registerActivityLifecycleCallbacks(mActivityLifecycleCallbacks)

        val networkCallback = NetworkCallbackImpl()
        val builder = NetworkRequest.Builder()
        val request = builder.build()
        val connMgr = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        connMgr?.registerNetworkCallback(request, networkCallback)

    }

    private fun ininState() {
        WonderSp.setDataDownState(this, 0);

    }

    private val mActivityLifecycleCallbacks = object : Application.ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
           KLog.d(TAG, "onCreated: " + activity.componentName.className)
        }

        override fun onActivityStarted(activity: Activity) {
            KLog.d(TAG, "onStart: " + activity.componentName.className)
        }

        override fun onActivityResumed(activity: Activity) {

        }

        override fun onActivityPaused(activity: Activity) {

        }

        override fun onActivityStopped(activity: Activity) {

        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

        }

        override fun onActivityDestroyed(activity: Activity) {
            KLog.d(TAG, "onDestroy: " + activity.componentName.className)
        }
    }


    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onTerminate() {
        // 程序终止的时候执行
        super.onTerminate()
        LoopJobService.stopDaemon(this)
    }

    override fun onLowMemory() {
        // 低内存的时候执行
        super.onLowMemory()
    }

    override fun onTrimMemory(level: Int) {
        // 程序在内存清理的时候执行
        super.onTrimMemory(level)
    }
}